package com.ruoyi.goods.service.impl;

import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.goods.controller.OrderController;
import com.ruoyi.goods.domain.BmGoods;
import com.ruoyi.goods.domain.BmPreferential;
import com.ruoyi.goods.domain.BmSpecification;
import com.ruoyi.goods.mapper.SellerMapper;
import com.ruoyi.goods.service.SellerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author {那蓝桉}
 * @Date: 2023/08/11/ 11:47
 * @description
 */
@Service
public class SellerServiceImpl implements SellerService {

    @Autowired
    private SellerMapper sellerMapper;
    @Resource
    private RedisService redisService;
    @Resource
    private OrderController orderController;
    @Override
    public List<BmGoods> queryAll() {
        String key = "goodsList:queryAll";
        List<BmGoods> goodsList =redisService.getCacheObject(key);
        if( goodsList == null || goodsList.size()==0){
            goodsList = sellerMapper.queryAll();
            redisService.setCacheObject(key,goodsList);
            Integer ttl = 0;
            for (BmGoods goods : goodsList) {
                queryById(goods.getBmSpecificationList().get(0).getId());
                Long stock = null;
                Long id = null;
                for (BmSpecification bmSpecification : goods.getBmSpecificationList()) {
//                    每个规格的商品的库存
                     stock = bmSpecification.getStock();
                }
                for (BmSpecification bmSpecification : goods.getBmSpecificationList()) {
//                    每个规格商品的id
                     id = bmSpecification.getId();
                }
//                如果有该key，删除并更新
                Boolean b = redisService.hasKey("list:" + id.toString());
                if (b){
                    redisService.deleteObject("list:"+id.toString());
                }
//                key为id,value为stock,可以直接执行redis.decr()；一条语句进行原子性操作，使用redis原子性递减控制请求人数
                redisService.setCacheObject("list:"+id.toString(),stock);

                Long endTime = goods.getBmPreferentiallist().get(0).getEndTime().getTime();
                Long now = new Date().getTime();
                ttl = (int) (endTime-now);
                System.out.println("endTime="+endTime+"now="+now+"ttl=" + ttl);
            }
            if(ttl>=0) {
                orderController.sendDelayedMsg("定时下架", ttl);
            }
        }
        return goodsList;
    }

    @Override
    public BmGoods queryById(Long id) {
        String key = "goods:queryById" + id;
        BmGoods goods = redisService.getCacheObject(key);
        if( goods == null ){
            goods = sellerMapper.queryById(id);
            redisService.setCacheObject(key,goods);
        }
        return goods;
    }

    @Override
    public List<BmPreferential> queryByStartTime() {
        return sellerMapper.queryByStartTime();
    }

    @Override
    public int updateStatus(Long id) {
        return sellerMapper.updateStatus(id);
    }

    @Override
    public int updateStatus0(Long id) {
        return sellerMapper.updateStatus0(id);
    }

    @Override
    public int decrStockCount(BmSpecification specification) {
        return sellerMapper.decrStock(specification);
    }
}
